package Frame;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.*;

import Function.FCEventLibrary;
import Function.FCItemLibrary;
import Function.FCMapLibrary;
import VWorldGameStudio.FCBattleEventOrder;
import VWorldGameStudio.FCEvent;
import VWorldGameStudio.FCItem;
import VWorldGameStudio.events.*;
import com.google.gson.Gson;

/**
 * 编辑或新建事件窗口。如地图跳转、商店、箱子、对话、留声机等
 */
public class FCEditEvent extends JDialog {

    private Vector<String> itemId = new Vector<>();

    private Vector<Integer> itemPrice = new Vector<>();

    private Vector<String> saytextv = new Vector<>();

    private Vector<String> phonographName = new Vector<>();

    private Vector<Integer> phonographId = new Vector<>();

    private int musicNum = 31;
    private ArrayList<String> eventMonsterIds = new ArrayList<>(0);
    private ArrayList<FCBattleEventOrder> eventOrders = new ArrayList<>(0);
    JPanel orderP;

    public FCEditEvent(boolean isNew, FCEventLibrary eventLib, FCMapLibrary mapLib, FCItemLibrary itemLib) {

        setTitle((isNew) ? "事件管理-添加事件" : "事件管理-编辑事件");
        setModal(true);
        setBounds(0, 0, 920, 525);
        setLayout(null);
        setLocationRelativeTo(null);
        setResizable(false);

        JLabel a0 = new JLabel("事件ID:");
        a0.setBounds(10, 10, 60, 20);
        this.add(a0);

        JComboBox<String> a1 = new JComboBox<String>();
        a1.setBounds(70, 10, 230, 20);
        a1.setEnabled(!isNew);
        if (isNew) {
            a1.addItem(String.valueOf(eventLib.getEventNum()));
        } else {
            ArrayList<String> eventsByTYpe = eventLib.getEventsByType("jump");
            for (String id: eventsByTYpe) {
                a1.addItem(id);
            }
        }
        this.add(a1);

        JLabel b0 = new JLabel("事件名称:");
        b0.setBounds(310, 10, 60, 20);
        this.add(b0);

        JTextField b1 = new JTextField();
        b1.setBounds(370, 10, 130, 20);
        b1.setEditable(isNew);
        this.add(b1);

        JLabel c0 = new JLabel("事件类型:");
        c0.setBounds(500, 10, 60, 20);
        this.add(c0);

        JComboBox<String> c1 = new JComboBox<String>();
        c1.setBounds(560, 10, 130, 20);
        for(String type: eventLib.allEventTypes){
            c1.addItem(type);
        }
        this.add(c1);

        JLabel miaoshu = new JLabel("注释描述:");
        miaoshu.setBounds(700, 10, 90, 20);
        this.add(miaoshu);
        JTextArea miaoshue = new JTextArea();
        miaoshue.setLineWrap(true);
        miaoshue.setWrapStyleWord(true);
        miaoshue.setBounds(710, 40, 200, 410);
        this.add(miaoshue);


        // 跳转控件
        JLabel d0 = new JLabel("目 的 地:");
        d0.setBounds(10, 40, 60, 20);
        d0.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        this.add(d0);

        JComboBox<String> d1 = new JComboBox<String>();
        d1.setBounds(70, 40, 130, 20);
        d1.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        for (int i = 0; i < mapLib.getMapNum(); i++) {
            d1.addItem(mapLib.getMapNameOfID(i));
        }
        this.add(d1);

        JLabel e0 = new JLabel("坐标X:");
        e0.setBounds(210, 40, 60, 20);
        e0.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        this.add(e0);

        JComboBox<String> e1 = new JComboBox<String>();
        e1.setBounds(270, 40, 130, 20);
        e1.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        if (c1.getSelectedIndex() == 0) {
            for (int i = 0; i < mapLib.getMapOfName(mapLib.getMapNameOfID(d1.getSelectedIndex())).getMapWidth(); i++) {
                e1.addItem(String.valueOf(i));
            }
        }
        this.add(e1);

        JLabel f0 = new JLabel("坐标Y:");
        f0.setBounds(410, 40, 60, 20);
        f0.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        this.add(f0);

        JComboBox<String> f1 = new JComboBox<String>();
        f1.setBounds(470, 40, 130, 20);
        f1.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        if (c1.getSelectedIndex() == 0) {
            for (int i = 0; i < mapLib.getMapOfName(mapLib.getMapNameOfID(d1.getSelectedIndex())).getMapWidth(); i++) {
                f1.addItem(String.valueOf(i));
            }
        }
        this.add(f1);

        JLabel g0 = new JLabel("音效编号:");
        g0.setBounds(10, 70, 60, 20);
        g0.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        this.add(g0);

        JComboBox<String> g1 = new JComboBox<String>();
        g1.setBounds(70, 70, 130, 20);
        g1.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        for (int i = 0; i < musicNum; i++) {
            g1.addItem(String.valueOf(i));
        }
        this.add(g1);

        JLabel h0 = new JLabel("音乐编号:");
        h0.setBounds(210, 70, 60, 20);
        h0.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        this.add(h0);

        JComboBox<String> h1 = new JComboBox<String>();
        h1.setBounds(270, 70, 130, 20);
        h1.setVisible((c1.getSelectedIndex() == 0) ? true : false);
        for (int i = 0; i < musicNum; i++) {
            h1.addItem(String.valueOf(i));
        }
        this.add(h1);

        // 商店控件
        JLabel shopisvendingtip = new JLabel("是否售货机:");
        shopisvendingtip.setBounds(10, 40, 80, 20);
        this.add(shopisvendingtip);

        JComboBox<String> shopisvending = new JComboBox<>();
        shopisvending.setBounds(80, 40, 120, 20);
        shopisvending.addItem("true");
        shopisvending.addItem("false");
        this.add(shopisvending);

        JLabel shopitemlisttip = new JLabel("添加的商品:");
        shopitemlisttip.setBounds(210, 40, 80, 20);
        this.add(shopitemlisttip);

        JComboBox<String> shopitemlist = new JComboBox<>();
        shopitemlist.setBounds(280, 40, 250, 20);
        for (int i = 0; i < itemLib.getItemNewID(); i++) {
            shopitemlist.addItem(itemLib.getNameOfNum(i));
        }
        this.add(shopitemlist);

        JLabel shoppricelisttip = new JLabel("售价:");
        shoppricelisttip.setBounds(530, 40, 30, 20);
        this.add(shoppricelisttip);
        JTextField shoppricelist = new JTextField();
        shoppricelist.setBounds(560, 40, 60, 20);
        this.add(shoppricelist);


        JButton shopadditem = new JButton("添加商品");
        shopadditem.setBounds(410, 70, 190, 20);
        this.add(shopadditem);
        JButton shopadditemA = new JButton("全部添加");
        shopadditemA.setBounds(605, 70, 80, 20);
        this.add(shopadditemA);

        JPanel shopitemtable = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                g.setColor(Color.white);
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                g.setColor(new Color(244, 244, 244));
                g.fillRect(1, 1, this.getWidth() - 3, 18);
                g.setColor(Color.black);
                g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
                g.drawString("物品id", 20, 16);
                g.drawString("价格", 270, 16);
                g.drawLine(0, 20, this.getWidth(), 20);
                for (int i = 0; i < itemId.size(); i++) {
                    g.drawString(itemId.get(i), 20, 36 + 20 * i);
                    g.drawString(String.valueOf(itemPrice.get(i)), 270, 36 + 20 * i);
                    g.drawLine(0, 20 * (i + 2), this.getWidth(), 20 * (i + 2));
                }
                g.drawLine(250, 0, 250, 20 + itemId.size() * 20);
            }
        };
        shopitemtable.setBounds(10, 70, 390, 240);
        this.add(shopitemtable);

        JLabel shopdeleteitem = new JLabel("删除商品:");
        shopdeleteitem.setBounds(410, 100, 80, 20);
        this.add(shopdeleteitem);

        JComboBox<Integer> shopdeleteitemid = new JComboBox<>();
        shopdeleteitemid.setBounds(480, 100, 120, 20);
        for (int i = 0; i < itemId.size(); i++) {
            shopdeleteitemid.addItem(i);
        }
        this.add(shopdeleteitemid);

        JButton shopdeleteitenok = new JButton("确定删除");
        shopdeleteitenok.setBounds(410, 130, 190, 20);
        this.add(shopdeleteitenok);

        JLabel shoprewardtip = new JLabel("售货机奖励物品:");
        shoprewardtip.setBounds(410, 160, 190, 20);

        this.add(shoprewardtip);
        JComboBox<String> shoprewardid = new JComboBox<>();
        shoprewardid.setBounds(410, 190, 190, 20);
        shoprewardid.addItem("null");
        for (int i = 0; i < itemLib.getItemNewID(); i++) {
            shoprewardid.addItem(itemLib.getNameOfNum(i));
        }
        this.add(shoprewardid);

        JLabel shopType2 = new JLabel("商店类型:");
        shopType2.setBounds(410, 220, 80, 20);
        this.add(shopType2);
        shopType2.setVisible(false);
        JComboBox<String> shopType2box = new JComboBox<String>();
        shopType2box.setBounds(490, 220, 90, 20);
        shopType2box.addItem("默认");
        shopType2box.addItem("出租战车");
        shopType2box.addItem("悬赏店");
        shopType2box.addItem("回收店");
        this.add(shopType2box);
        shopType2box.setVisible(false);
        shopType2box.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                switch (shopType2box.getSelectedIndex()) {
                    case 0:
                        shopitemlist.removeAllItems();
                        for (int i = 0; i < itemLib.getItemNewID(); i++) {
                            shopitemlist.addItem(itemLib.getNameOfNum(i));
                        }
                        shopitemlist.updateUI();
                        shoppricelisttip.setVisible(false);
                        shoppricelist.setVisible(false);
                        break;
                    case 1:
                        shopitemlist.removeAllItems();
                        for (String tankName : FCGMaster.tanksLibrary.getTankName()) {
                            shopitemlist.addItem(tankName);
                        }
                        shopitemlist.updateUI();
                        shoppricelisttip.setVisible(true);
                        shoppricelist.setVisible(true);
                        break;
                    case 2:
                    case 3:
                        shopitemlist.removeAllItems();
                        for (String name : itemLib.getWantedPic()) {
                            shopitemlist.addItem(name);
                        }
                        shopitemlist.updateUI();
                        shoppricelisttip.setVisible(false);
                        shoppricelist.setVisible(false);
                        break;

                }
            }
        });
        shopadditem.setVisible(false);
        shopadditemA.setVisible(false);
        shopdeleteitem.setVisible(false);
        shopdeleteitemid.setVisible(false);
        shopdeleteitenok.setVisible(false);
        shopisvending.setVisible(false);
        shopisvendingtip.setVisible(false);
        shopitemlist.setVisible(false);
        shopitemlisttip.setVisible(false);
        shopitemtable.setVisible(false);
        shoppricelist.setVisible(false);
        shoppricelisttip.setVisible(false);
        shoprewardtip.setVisible(false);
        shoprewardid.setVisible(false);

        // 箱子
        JLabel casemoneyt = new JLabel("奖励金额");
        casemoneyt.setBounds(10, 40, 60, 20);
        this.add(casemoneyt);

        JTextField casemoney = new JTextField("0");
        casemoney.setBounds(70, 40, 130, 20);
        this.add(casemoney);

        JLabel caseitem1t = new JLabel("物品1");
        caseitem1t.setBounds(210, 40, 60, 20);
        this.add(caseitem1t);

        JComboBox<String> caseitem1 = new JComboBox<>();
        caseitem1.setBounds(270, 40, 130, 20);
        caseitem1.addItem("null");
        for (int i = 0; i < itemLib.getItemNewID(); i++) {
            caseitem1.addItem(itemLib.getNameOfNum(i));
        }
        this.add(caseitem1);

        JLabel caseitem2t = new JLabel("物品2");
        caseitem2t.setBounds(410, 40, 60, 20);
        this.add(caseitem2t);

        JComboBox<String> caseitem2 = new JComboBox<>();
        caseitem2.setBounds(470, 40, 130, 20);
        caseitem2.addItem("null");
        for (int i = 0; i < itemLib.getItemNewID(); i++) {
            caseitem2.addItem(itemLib.getNameOfNum(i));
        }
        this.add(caseitem2);

        JLabel caseitem3t = new JLabel("物品3");
        caseitem3t.setBounds(10, 70, 60, 20);
        this.add(caseitem3t);

        JComboBox<String> caseitem3 = new JComboBox<>();
        caseitem3.setBounds(70, 70, 130, 20);
        caseitem3.addItem("null");
        for (int i = 0; i < itemLib.getItemNewID(); i++) {
            caseitem3.addItem(itemLib.getNameOfNum(i));
        }
        this.add(caseitem3);

        JLabel caseitem4t = new JLabel("物品4");
        caseitem4t.setBounds(210, 70, 60, 20);
        this.add(caseitem4t);

        JComboBox<String> caseitem4 = new JComboBox<>();
        caseitem4.setBounds(270, 70, 130, 20);
        caseitem4.addItem("null");
        for (int i = 0; i < itemLib.getItemNewID(); i++) {
            caseitem4.addItem(itemLib.getNameOfNum(i));
        }
        this.add(caseitem4);

        caseitem1.setVisible(false);
        caseitem2.setVisible(false);
        caseitem3.setVisible(false);
        caseitem4.setVisible(false);
        caseitem1t.setVisible(false);
        caseitem2t.setVisible(false);
        caseitem3t.setVisible(false);
        caseitem4t.setVisible(false);
        casemoney.setVisible(false);
        casemoneyt.setVisible(false);

        // npc对话
        JLabel saytextt = new JLabel("要添加的对话内容:");
        saytextt.setBounds(10, 40, 200, 20);
        this.add(saytextt);

        JTextArea saytext = new JTextArea();
        saytext.setBounds(10, 70, 690, 50);
        saytext.setLineWrap(true);
        saytext.setWrapStyleWord(true);
        this.add(saytext);

        JButton saytextok = new JButton("添加");
        saytextok.setBounds(510, 40, 90, 20);
        this.add(saytextok);

        JPanel saytexttable = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                g.setColor(Color.white);
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                g.setColor(new Color(244, 244, 244));
                g.fillRect(1, 1, this.getWidth() - 3, 18);
                g.setColor(Color.black);
                g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
                g.drawString("num", 10, 16);
                g.drawString("内容", 100, 16);
                g.drawLine(0, 20, this.getWidth(), 20);
                for (int i = 0; i < saytextv.size(); i++) {
                    g.drawString(String.valueOf(i), 10, 36 + 20 * i);
                    g.drawString(saytextv.get(i), 50, 36 + 20 * i);
                    g.drawLine(0, 20 * (i + 2), this.getWidth(), 20 * (i + 2));
                }
                g.drawLine(40, 0, 40, 20 + itemId.size() * 20);
            }
        };
        saytexttable.setBounds(10, 130, 390, 200);
        this.add(saytexttable);

        JLabel saytextTip = new JLabel("示例：sistersay1<option1 id='option1text'><option2 id='option2text'>");
        saytextTip.setBounds(10, 340, 600, 20);
        this.add(saytextTip);
        saytextTip.setVisible(false);
        JLabel saytextTip2 = new JLabel("表示读取language/ch.vw的文字和选项文字");
        saytextTip2.setBounds(10, 360, 600, 20);
        this.add(saytextTip2);
        saytextTip2.setVisible(false);
        JLabel saytextdelt = new JLabel("删除对话:");
        saytextdelt.setBounds(410, 130, 80, 20);
        this.add(saytextdelt);

        JComboBox<Integer> saytextdelid = new JComboBox<>();
        saytextdelid.setBounds(480, 130, 120, 20);
        for (int i = 0; i < saytextv.size(); i++) {
            saytextdelid.addItem(i);
        }
        this.add(saytextdelid);

        JButton saytextdelok = new JButton("确定删除");
        saytextdelok.setBounds(410, 160, 190, 20);
        this.add(saytextdelok);

        JButton showSayOk = new JButton("显示对话");
        showSayOk.setBounds(410, 190, 190, 20);
        this.add(showSayOk);

        saytext.setVisible(false);
        saytextt.setVisible(false);
        saytextok.setVisible(false);
        saytexttable.setVisible(false);
        saytextdelid.setVisible(false);
        saytextdelok.setVisible(false);
        saytextdelt.setVisible(false);
        showSayOk.setVisible(false);
        // 留声机
        JLabel phnamet = new JLabel("音乐名称:");
        phnamet.setBounds(10, 40, 80, 20);
        this.add(phnamet);

        JTextField phname = new JTextField();
        phname.setBounds(70, 40, 130, 20);
        this.add(phname);

        JLabel phnumt = new JLabel("音乐编号:");
        phnumt.setBounds(210, 40, 80, 20);
        this.add(phnumt);

        JComboBox<String> phnum = new JComboBox<>();
        phnum.setBounds(270, 40, 130, 20);
        for (int i = 0; i < musicNum; i++) {
            phnum.addItem(String.valueOf(i));
        }
        this.add(phnum);

        JButton phadd = new JButton("添加");
        phadd.setBounds(410, 40, 190, 20);
        this.add(phadd);

        JPanel phtable = new JPanel() {
            @Override
            public void paintComponent(Graphics g) {
                g.setColor(Color.white);
                g.fillRect(0, 0, this.getWidth(), this.getHeight());
                g.setColor(new Color(244, 244, 244));
                g.fillRect(1, 1, this.getWidth() - 3, 18);
                g.setColor(Color.black);
                g.drawRect(0, 0, this.getWidth() - 1, this.getHeight() - 1);
                g.drawString("num", 10, 16);
                g.drawString("内容", 100, 16);
                g.drawLine(0, 20, this.getWidth(), 20);
                for (int i = 0; i < phonographId.size(); i++) {
                    g.drawString(String.valueOf(i), 10, 36 + 20 * i);
                    g.drawString("音乐名:" + phonographName.get(i) + " 编号:" + phonographId.get(i), 50, 36 + 20 * i);
                    g.drawLine(0, 20 * (i + 2), this.getWidth(), 20 * (i + 2));
                }
                g.drawLine(40, 0, 40, 20 + phonographId.size() * 20);
            }
        };
        phtable.setBounds(10, 70, 390, 200);
        this.add(phtable);

        JLabel phdelt = new JLabel("删除音乐:");
        phdelt.setBounds(410, 70, 80, 20);
        this.add(phdelt);

        JComboBox<Integer> phdelid = new JComboBox<>();
        phdelid.setBounds(480, 70, 120, 20);
        for (int i = 0; i < phonographName.size(); i++) {
            phdelid.addItem(i);
        }
        this.add(phdelid);

        JButton phdelok = new JButton("确定删除");
        phdelok.setBounds(410, 100, 190, 20);
        this.add(phdelok);

        phadd.setVisible(false);
        phdelid.setVisible(false);
        phdelok.setVisible(false);
        phdelt.setVisible(false);
        phname.setVisible(false);
        phnamet.setVisible(false);
        phnum.setVisible(false);
        phnumt.setVisible(false);
        phtable.setVisible(false);

        // 保存数据
        JButton ok = new JButton("确认");
        ok.setBounds(10, 450, 590, 20);
        this.add(ok);

        phadd.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                phonographName.add(phname.getText());
                phonographId.add(phnum.getSelectedIndex());
                phdelid.removeAllItems();
                for (int i = 0; i < phonographName.size(); i++)
                    phdelid.addItem(i);
                phtable.updateUI();
            }
        });

        phdelok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int a = phdelid.getSelectedIndex();
                phonographId.remove(a);
                phonographName.remove(a);
                phtable.updateUI();
            }
        });


        d1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (c1.getSelectedIndex() == 0) {
                    e1.removeAllItems();
                    f1.removeAllItems();
                    for (int i = 0; i < mapLib.getMapOfName(mapLib.getMapNameOfID(d1.getSelectedIndex()))
                            .getMapWidth(); i++)
                        e1.addItem(String.valueOf(i));
                    for (int i = 0; i < mapLib.getMapOfName(mapLib.getMapNameOfID(d1.getSelectedIndex()))
                            .getMapHeight(); i++)
                        f1.addItem(String.valueOf(i));
                }
            }
        });
        //剧情战斗
        JLabel battle1 = new JLabel("选择怪物:");
        battle1.setBounds(10, 40, 80, 20);
        this.add(battle1);
        battle1.setVisible(false);
        JComboBox<String> battle1box = new JComboBox<>();
        battle1box.setBounds(100, 40, 130, 20);
        battle1box.addItem("null");
        for (String s : FCGMaster.monsterLibrary.monstername) {
            battle1box.addItem(s);
        }

        this.add(battle1box);
        JLabel r = new JLabel("+", JLabel.CENTER);
        r.setFont(new Font("黑体", 0, 18));
        r.setBorder(BorderFactory.createLineBorder(Color.black));
        r.setBounds(235, 40, 20, 20);
        this.add(r);
        r.setVisible(false);
        battle1box.setVisible(false);


        JLabel battleSay = new JLabel("选择对话:");
        battleSay.setBounds(10, 180, 60, 20);
        this.add(battleSay);
        battleSay.setVisible(false);
        JComboBox<String> battleSayBox = new JComboBox<String>();
        battleSayBox.setBounds(75, 180, 150, 20);
        battleSayBox.addItem("null");
        for (String sayId : FCGMaster.eventLibrary.getAllSayEvent()) {
            battleSayBox.addItem(sayId);
        }
        this.add(battleSayBox);
        battleSayBox.setVisible(false);
        JButton addOrder = new JButton("添加操控指令");
        addOrder.setBounds(10, 220, 140, 20);
        this.add(addOrder);
        addOrder.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new BattleEventInfo();
            }
        });
        addOrder.setVisible(false);
        orderP = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                g.setColor(Color.white);
                g.fillRect(0, 0, 500, 200);
                g.setColor(Color.black);
                int i = 0;
                for (FCBattleEventOrder o : eventOrders) {
                    g.drawString(o.toString(), 5, i * 20 + 17);
                    g.drawLine(0, i * 20 + 20, 500, i * 20 + 20);
                    g.setColor(Color.white);
                    g.fillRect(480, i * 20 + 1, 20, 18);
                    g.setColor(Color.black);
                    g.drawLine(484, i * 20 + 10, 493, i * 20 + 10);
                    i++;
                }
            }
        };
        orderP.setBounds(10, 245, 500, 200);
        orderP.setBorder(BorderFactory.createLineBorder(Color.black));
        this.add(orderP);
        orderP.setVisible(false);
        orderP.addMouseListener(new FCEditAnimation.Mlistener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getX() > 480) {
                    int a = e.getY() / 20;
                    if (eventOrders.size() > a) {
                        eventOrders.remove(a);
                        orderP.updateUI();
                    }
                }
            }
        });
        JPanel battlep = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                g.setColor(Color.white);
                g.fillRect(0, 0, 200, 200);
                g.setColor(Color.black);
                int i = 0;
                for (String name : eventMonsterIds) {
                    g.drawString(name, 5, i * 20 + 17);
                    g.drawLine(0, i * 20 + 20, 200, i * 20 + 20);
                    g.setColor(Color.white);
                    g.fillRect(180, i * 20 + 1, 20, 18);
                    g.setColor(Color.black);
                    g.drawLine(184, i * 20 + 10, 193, i * 20 + 10);
                    i++;
                }
            }
        };
        battlep.setBounds(260, 40, 200, 200);
        battlep.setBorder(BorderFactory.createLineBorder(Color.black));
        this.add(battlep);
        battlep.setVisible(false);
        battlep.addMouseListener(new FCEditAnimation.Mlistener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getX() > 180) {
                    int a = e.getY() / 20;
                    if (eventMonsterIds.size() > a) {
                        eventMonsterIds.remove(a);
                        battlep.updateUI();
                    }
                }
            }
        });
        r.addMouseListener(new FCEditAnimation.Mlistener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (!battle1box.getSelectedItem().equals("null")) {
                    eventMonsterIds.add(battle1box.getSelectedItem().toString());
                    battlep.updateUI();
                }
            }
        });

        JLabel stepOnj = new JLabel("携带的事件:");
        stepOnj.setBounds(10, 40, 90, 20);
        this.add(stepOnj);
        stepOnj.setVisible(false);
        JComboBox<String> stepOnjBox = new JComboBox<String>();
        stepOnjBox.setBounds(110, 40, 200, 20);
        stepOnjBox.addItem("null");
        for (String eventId : FCGMaster.eventLibrary.getEntityname()) {
            stepOnjBox.addItem(eventId);
        }
        this.add(stepOnjBox);
        stepOnjBox.setVisible(false);
        JLabel stepOnjt = new JLabel("编写指令（参考代码EventCmdEntity类json）:");
        stepOnjt.setBounds(10, 70, 400, 20);
        this.add(stepOnjt);
        stepOnjt.setVisible(false);
        JTextArea stepCmdText = new JTextArea();
        stepCmdText.setBounds(10, 100, 590, 50);
        this.add(stepCmdText);
        stepCmdText.setVisible(false);
        c1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int num = c1.getSelectedIndex();
                d0.setVisible(num == 0);
                d1.setVisible(num == 0);
                e0.setVisible(num == 0);
                e1.setVisible(num == 0);
                f0.setVisible(num == 0);
                f1.setVisible(num == 0);
                g0.setVisible(num == 0);
                g1.setVisible(num == 0);
                h0.setVisible(num == 0);
                h1.setVisible(num == 0);
                shopadditem.setVisible(num == 1);
                shopadditemA.setVisible(num == 1);
                shopdeleteitem.setVisible(num == 1);
                shopdeleteitemid.setVisible(num == 1);
                shopdeleteitenok.setVisible(num == 1);
                shopisvending.setVisible(num == 1);
                shopisvendingtip.setVisible(num == 1);
                shopitemlist.setVisible(num == 1);
                shopitemlisttip.setVisible(num == 1);
                shopitemtable.setVisible(num == 1);
                shoppricelist.setVisible(num == 1);
                shoppricelisttip.setVisible(num == 1);
                shoprewardid.setVisible(num == 1);
                shoprewardtip.setVisible(num == 1);
                shopType2.setVisible(num == 1);
                shopType2box.setVisible(num == 1);
                caseitem1.setVisible(num == 2 || num == 19||num == 5);
                caseitem2.setVisible(num == 2);
                caseitem3.setVisible(num == 2);
                caseitem4.setVisible(num == 2);
                caseitem1t.setVisible(num == 2|| num == 19||num == 5);
                caseitem2t.setVisible(num == 2);
                caseitem3t.setVisible(num == 2);
                caseitem4t.setVisible(num == 2);
                casemoney.setVisible(num == 2|| num == 19);
                casemoneyt.setVisible(num == 2|| num == 19);
                saytext.setVisible(num == 3);
                saytextt.setVisible(num == 3);
                saytextok.setVisible(num == 3);
                saytexttable.setVisible(num == 3);
                saytextdelid.setVisible(num == 3);
                saytextdelok.setVisible(num == 3);
                saytextdelt.setVisible(num == 3);
                showSayOk.setVisible(num == 3);
                saytextTip.setVisible(num == 3);
                saytextTip2.setVisible(num == 3);
                phadd.setVisible(num == 4);
                phdelid.setVisible(num == 4);
                phdelok.setVisible(num == 4);
                phdelt.setVisible(num == 4);
                phname.setVisible(num == 4);
                phnamet.setVisible(num == 4);
                phnum.setVisible(num == 4);
                phnumt.setVisible(num == 4);
                phtable.setVisible(num == 4);

                battle1.setVisible(num == 7);
                battle1box.setVisible(num == 7);
                addOrder.setVisible(num == 7);
                orderP.setVisible(num == 7);
                r.setVisible(num == 7);
                battlep.setVisible(num == 7);
                battleSay.setVisible(num == 7 || num == 8);
                battleSayBox.setVisible(num == 7 || num == 8);

                stepOnjBox.setVisible(num == 9);
                stepCmdText.setVisible(num == 9);
                stepOnj.setVisible(num == 9);
                stepOnjt.setVisible(num == 9);

                if(!isNew){
                    a1.removeAllItems();
                    ArrayList<String> eventsByTYpe = eventLib.getEventsByType((String) c1.getSelectedItem());
                    for (String id: eventsByTYpe) {
                        a1.addItem(id);
                    }
                    a1.updateUI();
                }
            }
        });
        a1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if( a1.getSelectedItem() == null){
                    return;
                }
                FCEvent ev = eventLib.getEventOfName((String) a1.getSelectedItem());
                if(ev == null){
                    return;
                }
                Gson g = new Gson();
                System.out.println("选择 "+g.toJson(ev));
                miaoshue.setText(ev.getDescribe());
             //   c1.setSelectedItem(ev.getType());
                b1.setText(ev.getName());
                switch (c1.getSelectedIndex()) {
                    case 0:
                        JumpEvent jumpEvent = (JumpEvent)ev;
                        d1.setSelectedItem(jumpEvent.getDestination());
                        e1.setSelectedIndex(jumpEvent.getJumpx());
                        f1.setSelectedIndex(jumpEvent.getJumpy());
                        g1.setSelectedIndex(jumpEvent.getMusicnum0());
                        h1.setSelectedIndex(jumpEvent.getMusicnum1());
                        break;
                    case 1:
                        ShopEvent shopEvent = (ShopEvent)ev;
                        shopisvending.setSelectedItem(shopEvent.isIsvending());
                        itemId = shopEvent.getItemid();
                        itemPrice = shopEvent.getItemprice();
                        shopitemtable.updateUI();
                        shopdeleteitemid.removeAllItems();
                        for (int i = 0; i < itemId.size(); i++)
                            shopdeleteitemid.addItem(i);
                        if ("Taxi".equals(shopEvent.getType2())) {
                            shopType2box.setSelectedIndex(1);
                            shoppricelisttip.setVisible(true);
                            shoppricelist.setVisible(true);
                        } else if ("RewardShop".equals(shopEvent.getType2())) {
                            shopType2box.setSelectedIndex(2);
                            shoppricelisttip.setVisible(false);
                            shoppricelist.setVisible(false);
                        } else if ("buyOutShop".equals(shopEvent.getType2())) {
                            shopType2box.setSelectedIndex(3);
                            shoppricelisttip.setVisible(false);
                            shoppricelist.setVisible(false);
                        } else {
                            shoppricelisttip.setVisible(false);
                            shoppricelist.setVisible(false);
                            shopType2box.setSelectedIndex(0);
                        }
                        shopType2box.updateUI();
                        break;
                    case 2:
                        CaseEvent caseEvent = (CaseEvent)ev;
                        casemoney.setText(String.valueOf(caseEvent.getCaseMoney()));
                        caseitem1.setSelectedItem(caseEvent.getCaseitem()[0]);
                        caseitem2.setSelectedItem(caseEvent.getCaseitem()[1]);
                        caseitem3.setSelectedItem(caseEvent.getCaseitem()[2]);
                        caseitem4.setSelectedItem(caseEvent.getCaseitem()[3]);
                        break;
                    case 3:
                        SayEvent sayEvent = (SayEvent)ev;
                        saytextv = sayEvent.getSaytextBySelectNum(0);
                        saytextdelid.removeAllItems();
                        for (int i = 0; i < saytextv.size(); i++) {
                            saytextdelid.addItem(i);
                        }
                        saytexttable.updateUI();
                        break;
                    case 5:
                        DoorEvent doorEvent = (DoorEvent) ev;
                        caseitem1.setSelectedItem(doorEvent.getNeedItemKey());
                        break;
                    case 7:
                        BattleEvent battleEvent = (BattleEvent)ev;
                        eventOrders.clear();
                        eventOrders.addAll(battleEvent.getEventOrders());
                        orderP.updateUI();
                        eventMonsterIds.clear();
                        eventMonsterIds.addAll(battleEvent.getMonsterId());
                        battleSayBox.setSelectedItem(battleEvent.getContainSayTextId() == null ? "null" : battleEvent.getContainSayTextId());
                        battleSayBox.updateUI();
                        battlep.updateUI();
                        break;
                    case 8:
                        ItemCtrlEvent ctrlEvent = (ItemCtrlEvent)ev;
                        battleSayBox.setSelectedItem(ctrlEvent.getContainSayTextId() == null ? "null" : ctrlEvent.getContainSayTextId());
                        battleSayBox.updateUI();
                        break;
                    case 9:
                        StepOnEvent stepOnEvent = (StepOnEvent) ev;
                        if (stepOnEvent.getContainOtherEventId() != null) {
                            stepOnjBox.setSelectedItem(stepOnEvent.getContainOtherEventId());
                        }
                        stepCmdText.setText(stepOnEvent.getContainCommand());
                        break;
                    case 19:
                        MapHiddenEvent mapHiddenEvent = (MapHiddenEvent) ev;
                        caseitem1.setSelectedItem(mapHiddenEvent.getHiddenitemId());
                        casemoney.setText(""+mapHiddenEvent.getMoney());
                        break;
                }
            }
        });


        saytextok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String as = saytext.getText();
                saytext.setText("");
                if (as != null && as.length() > 0) {
                    saytextv.add(as);
                    saytexttable.updateUI();
                    saytextdelid.removeAllItems();
                    for (int i = 0; saytextv.size() > i; i++)
                        saytextdelid.addItem(i);
                }
            }
        });

        saytextdelok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                saytextv.remove(saytextdelid.getSelectedIndex());
                saytexttable.updateUI();
                saytextdelid.removeAllItems();
                for (int i = 0; saytextv.size() > i; i++)
                    saytextdelid.addItem(i);
            }
        });

        shopadditem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id = shopitemlist.getSelectedItem().toString();
                String price = shoppricelist.getText().length() >0? shoppricelist.getText() : "1";
                //价格已经固定在excel表了，
                itemId.add(id);
                itemPrice.add(Integer.valueOf(price));
                shopitemtable.updateUI();
                shopdeleteitemid.removeAllItems();
                for (int i = 0; i < itemId.size(); i++)
                    shopdeleteitemid.addItem(i);
            }
        });
        shopadditemA.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //价格已经固定在excel表了，
                itemId.clear();
                itemPrice.clear();
                for(String id: itemLib.getItemName()){
                    FCItem m = itemLib.getItemOfID(id);
                    if(m.getBuyInPrice() > 0) {
                        itemId.add(id);
                        itemPrice.add(1);
                    }
                }
                shopitemtable.updateUI();
                shopdeleteitemid.removeAllItems();
                for (int i = 0; i < itemId.size(); i++)
                    shopdeleteitemid.addItem(i);
            }
        });

        shopdeleteitenok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int num = shopdeleteitemid.getSelectedIndex();
                itemId.remove(num);
                itemPrice.remove(num);
                shopitemtable.updateUI();
                shopdeleteitemid.removeAllItems();
                for (int i = 0; i < itemId.size(); i++)
                    shopdeleteitemid.addItem(i);
            }
        });

        showSayOk.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
              String s =  saytextv.get(saytextdelid.getSelectedIndex());
              saytext.setText(s);
              saytext.updateUI();
            }
        });

        ok.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (b1.getText() != null && !b1.getText().equals("")) {
                    String type2 = "def";
                    if (shopType2box.getSelectedIndex() == 1) {
                        type2 = "Taxi";
                    } else if (shopType2box.getSelectedIndex() == 2) {
                        type2 = "RewardShop";
                    }else if (shopType2box.getSelectedIndex() == 3) {
                        type2 = "buyOutShop";
                    }

                    eventLib.registerEntity(b1.getText(), c1.getSelectedItem().toString(), d1.getSelectedItem().toString(),
                            e1.getSelectedIndex(), f1.getSelectedIndex(), g1.getSelectedIndex(), h1.getSelectedIndex(),
                            (shopisvending.getSelectedIndex() == 0) ? true : false, itemId, itemPrice,
                            shoprewardid.getSelectedItem().toString(), Long.valueOf(casemoney.getText()),
                            caseitem1.getSelectedItem().toString(), caseitem2.getSelectedItem().toString(),
                            caseitem3.getSelectedItem().toString(), caseitem4.getSelectedItem().toString(), saytextv,
                            phonographName, phonographId, eventOrders, eventMonsterIds, (String) battleSayBox.getSelectedItem(),
                            stepCmdText.getText(), (String) stepOnjBox.getSelectedItem(), type2, miaoshue.getText());
                    new FCTip((isNew) ? "新增事件" : "编辑事件", "设置成功!");
                    dispose();
                } else
                    new FCTip((isNew) ? "新增事件" : "编辑事件", "事件名不得为空!");
            }
        });

        setVisible(true);
    }


    private class BattleEventInfo extends JDialog {
        int[] types = new int[]{1, 2, 3, 4};

        public BattleEventInfo() {
            setBounds(0, 0, 500, 300);
            setTitle("剧情战斗编排");
            setModal(true);
            setLocationRelativeTo(null);
            setLayout(null);
            setResizable(false);

            JComboBox<String> type = new JComboBox<String>();
            type.setBounds(10, 20, 110, 20);
            type.addItem("对话指令");
            type.addItem("操纵指令");
            type.addItem("属性指令");
            type.addItem("结束跳转剧情");
            this.add(type);

            JLabel b0 = new JLabel("第几回合:");
            b0.setBounds(140, 20, 80, 20);
            this.add(b0);

            JTextField b1 = new JTextField();
            b1.setBounds(220, 20, 60, 20);
            this.add(b1);
            JLabel b2 = new JLabel("(-1表示战斗结束结算前)");
            b2.setBounds(285, 20, 140, 20);
            this.add(b2);

            JLabel say = new JLabel("选择对话:");
            say.setBounds(10, 50, 80, 20);
            this.add(say);

            JComboBox<String> saybox = new JComboBox<String>();
            saybox.setBounds(90, 50, 200, 20);
            saybox.addItem("null");
            for (String sayId : FCGMaster.eventLibrary.getAllSayEvent()) {
                saybox.addItem(sayId);
            }
            this.add(saybox);

            JLabel plot = new JLabel("选择剧情:");
            plot.setBounds(10, 50, 80, 20);
            this.add(plot);
            plot.setVisible(false);

            JComboBox<String> plotbox = new JComboBox<String>();
            plotbox.setBounds(90, 50, 120, 20);
            plotbox.addItem("null");
            for (String pl : FCGMaster.plotLibrary.plotId) {
                plotbox.addItem(pl);
            }
            this.add(plotbox);
            plotbox.setVisible(false);

            JLabel ctrlType = new JLabel("操纵类型:");
            ctrlType.setBounds(10, 50, 80, 20);
            this.add(ctrlType);
            ctrlType.setVisible(false);
            JComboBox<String> ctrlTypebox = new JComboBox<String>();
            ctrlTypebox.setBounds(90, 50, 120, 20);
            ctrlTypebox.addItem("npc加入战斗");
            ctrlTypebox.addItem("npc退出战斗");
            ctrlTypebox.addItem("怪加入战斗");
            ctrlTypebox.addItem("怪退出战斗");
            ctrlTypebox.addItem("怪物只攻击");
            ctrlTypebox.addItem("怪物使用物品");
            ctrlTypebox.addItem("npc使用物品");
            this.add(ctrlTypebox);
            ctrlTypebox.setVisible(false);

            JLabel npc = new JLabel("选择npc:");
            npc.setBounds(10, 80, 80, 20);
            this.add(npc);
            npc.setVisible(false);
            JComboBox<String> npcbox = new JComboBox<String>();
            npcbox.setBounds(90, 80, 250, 20);
            npcbox.addItem("null");
            for (String name : FCGMaster.characterLibrary.characterName) {
                npcbox.addItem(name);
            }
            this.add(npcbox);
            npcbox.setVisible(false);

            JLabel monster = new JLabel("选择怪物:");
            monster.setBounds(10, 80, 200, 20);
            this.add(monster);
            monster.setVisible(false);
            JComboBox<String> monsterbox = new JComboBox<String>();
            monsterbox.setBounds(90, 80, 120, 20);
            monsterbox.addItem("null");
            for (String name : FCGMaster.monsterLibrary.monstername) {
                monsterbox.addItem(name);
            }
            this.add(monsterbox);
            monsterbox.setVisible(false);

            JLabel item = new JLabel("选择物品:");
            item.setBounds(10, 120, 200, 20);
            this.add(item);
            item.setVisible(false);
            JComboBox<String> itembox = new JComboBox<String>();
            itembox.setBounds(90, 120, 120, 20);
            itembox.addItem("null");
            for (String name : FCGMaster.itemLibrary.getItemName()) {
                itembox.addItem(name);
            }
            this.add(itembox);
            itembox.setVisible(false);

            type.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int t = type.getSelectedIndex() + 1;
                    plotbox.setVisible(t == 4);
                    plot.setVisible(t == 4);
                    say.setVisible(t == 1);
                    saybox.setVisible(t == 1);
                    ctrlType.setVisible(t == 2);
                    ctrlTypebox.setVisible(t == 2);
                    int cType = 0;
                    if (t == 2) {
                        cType = ctrlTypebox.getSelectedIndex() + 1;
                    }
                    npc.setVisible(cType == 1 || cType == 2 || cType == 5 || cType == 7);
                    npcbox.setVisible(cType == 1 || cType == 2 || cType == 5 || cType == 7);
                    monster.setVisible(cType == 3 || cType == 4 || cType == 6);
                    monsterbox.setVisible(cType == 3 || cType == 4 || cType == 6);
                    item.setVisible(cType == 6 || cType == 7);
                    itembox.setVisible(cType == 6 || cType == 7);
                }
            });
            ctrlTypebox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int cType = ctrlTypebox.getSelectedIndex() + 1;
                    npc.setVisible(cType == 1 || cType == 2 || cType == 5 || cType == 7);
                    npcbox.setVisible(cType == 1 || cType == 2 || cType == 5 || cType == 7);
                    monster.setVisible(cType == 3 || cType == 4 || cType == 6);
                    monsterbox.setVisible(cType == 3 || cType == 4 || cType == 6);
                    item.setVisible(cType == 6 || cType == 7);
                    itembox.setVisible(cType == 6 || cType == 7);
                }
            });

            JButton ok = new JButton("确认");
            ok.setBounds(10, 230, 480, 20);
            this.add(ok);
            ok.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    int t = types[type.getSelectedIndex()];
                    FCBattleEventOrder eventOrder = new FCBattleEventOrder(t);
                    try {
                        eventOrder.whtichRound = Integer.parseInt(b1.getText());
                    } catch (Exception e2) {
                    }
                    boolean createOk = true;
                    if (eventOrder.whtichRound == 0) {
                        createOk = false;
                    }
                    if (t == 1) {
                        if ("null".equals(saybox.getSelectedItem())) {
                            createOk = false;
                        } else {
                            eventOrder.battleSayTextId = (String) saybox.getSelectedItem();
                        }
                    } else if (t == 2) {
                        eventOrder.contrlNpc = (String) npcbox.getSelectedItem();
                        eventOrder.useItemId = (String) itembox.getSelectedItem();
                        eventOrder.contrlMonsterId = (String) monsterbox.getSelectedItem();

                        int childT = ctrlTypebox.getSelectedIndex() + 1;
                        eventOrder.eventOrderDetail = childT;
                        if (childT == 6 || childT == 7) {
                            if ("null".equals(itembox.getSelectedItem())) {
                                createOk = false;
                            }
                        }
                        switch (childT) {
                            case 3:
                            case 4:
                            case 6:
                                if ("null".equals(monsterbox.getSelectedItem())) {
                                    createOk = false;
                                }
                                break;
                            case 7:
                            case 1:
                            case 2:
                            case 5:
                                if ("null".equals(npcbox.getSelectedItem())) {
                                    createOk = false;
                                }
                                break;
                        }
                    } else if (t == 4) {
                        eventOrder.whtichRound = -1;
                        eventOrder.jumpPlotId = (String) plotbox.getSelectedItem();
                        if ("null".equals(plotbox.getSelectedItem())) {
                            createOk = false;
                        }
                    }
                    if (createOk) {
                        eventOrders.add(eventOrder);
                        orderP.updateUI();
                    }
                    System.out.println("eventOrders " + eventOrders.size() + " " + eventOrder.toString());
                    dispose();
                }
            });
            setVisible(true);
        }

    }
}
